package com.softeem.music_sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.entity.PlayRecord;
import com.softeem.music_sys.service.PlayRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 播放记录控制器
 */
@Tag(name = "播放记录接口", description = "播放记录相关操作:添加、查询、统计等")
@RestController
@RequestMapping("/play-record")
public class PlayRecordController {
    
    @Autowired
    private PlayRecordService playRecordService;

    @Operation(summary = "添加播放记录", description = "用户播放音乐时添加播放记录")
    @PostMapping
    public Result add(@RequestBody PlayRecord playRecord) {
        return playRecordService.addPlayRecord(playRecord);
    }

    @Operation(summary = "更新播放记录", description = "更新播放记录，设置结束时间")
    @PutMapping
    public Result update(@RequestBody PlayRecord playRecord) {
        return playRecordService.updatePlayRecord(playRecord);
    }

    @Operation(summary = "删除播放记录", description = "根据ID删除播放记录")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return playRecordService.deletePlayRecord(id);
    }

    @Operation(summary = "获取用户播放记录", description = "获取指定用户的播放记录")
    @GetMapping("/user/{userId}")
    public Result getByUser(@PathVariable Integer userId,
                           @RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer limit) {
        Page<PlayRecord> resultPage = playRecordService.getRecordsByUser(userId, page, limit);
        return Result.success().setData(resultPage);
    }

    @Operation(summary = "获取音乐播放记录", description = "获取某首音乐的播放记录")
    @GetMapping("/music/{musicId}")
    public Result getByMusic(@PathVariable Integer musicId,
                            @RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer limit) {
        Page<PlayRecord> resultPage = playRecordService.getRecordsByMusic(musicId, page, limit);
        return Result.success().setData(resultPage);
    }

    @Operation(summary = "获取用户最近播放", description = "获取用户最近播放的音乐记录")
    @GetMapping("/user/{userId}/recent")
    public Result getRecentByUser(@PathVariable Integer userId,
                                  @RequestParam(defaultValue = "20") Integer limit) {
        Page<PlayRecord> resultPage = playRecordService.getRecentRecordsByUser(userId, limit);
        return Result.success().setData(resultPage);
    }

    @Operation(summary = "统计用户播放次数", description = "统计用户播放某首音乐的次数")
    @GetMapping("/count")
    public Result countPlayTimes(@RequestParam Integer userId,
                                 @RequestParam Integer musicId) {
        long count = playRecordService.countPlayTimes(userId, musicId);
        return Result.success().setData(count);
    }

    @Operation(summary = "清空用户播放记录", description = "清空指定用户的所有播放记录")
    @DeleteMapping("/clear/{userId}")
    public Result clearByUser(@PathVariable Integer userId) {
        return playRecordService.clearUserRecords(userId);
    }

    @Operation(summary = "按时间范围查询播放记录", description = "查询指定时间范围内的播放记录")
    @Parameters({
            @Parameter(name = "userId", description = "用户ID"),
            @Parameter(name = "startDate", description = "开始时间"),
            @Parameter(name = "endDate", description = "结束时间")
    })
    @GetMapping("/time-range")
    public Result getByTimeRange(@RequestParam Integer userId,
                                 @RequestParam String startDate,
                                 @RequestParam String endDate,
                                 @RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer limit) {
        Page<PlayRecord> resultPage = playRecordService.getRecordsByTimeRange(userId, startDate, endDate, page, limit);
        return Result.success().setData(resultPage);
    }
}